package com.yanqu.road.server.cmd.zhaoshang;

import com.yanqu.road.entity.config.player.UserDecorationInfoResult;
import com.yanqu.road.entity.config.zhaoshang.ZsProjectInfo;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.gameplayer.module.zhaoshang.ZsModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.zhaoshang.ZsConfigMgr;
import com.yanqu.road.server.manger.zhaoshang.ZsMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ZS_PROJECT_SEAT_ROB, desc = "项目席位抢占")
public class ZsProjectSeatRobCmd extends AbstractCommand {

    private long projectId;
    private int seatId;
    private long seatUserId;
    private int templateId;
    private long projectServerId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ZhaoShangProto.ZsProjectSeatRobReqMsg reqMsg = ZhaoShangProto.ZsProjectSeatRobReqMsg.parseFrom(packet.getBytes());
        projectId = reqMsg.getProjectId();
        seatId = reqMsg.getSeatId();
        seatUserId = reqMsg.getSeatUserId();
        templateId = reqMsg.getTemplateId();
        projectServerId = reqMsg.getServerId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(!SystemOpenMgr.systemOpen(player, eSystemId.ZhaoShang.getValue())){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        if(!ZsMgr.isZsSystemOpenTime()){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        ZsProjectInfo zsProjectInfo = ZsConfigMgr.getZsProjectInfoMap().get(templateId);
        if(zsProjectInfo == null){
            ret = GameErrorCode.E_ZS_PROJECT_CONFIG_NOT_FOUND;
            return;
        }

        if (player.getModule(ZsModule.class).isOverRobTimes(seatUserId)) {
            ret = GameErrorCode.E_ZS_ROB_SAME_PLAYER_OVER_TIMES;
            return;
        }

        //userBaseInfo在前，检测下装饰过期
        UserBaseInfo userBaseInfo = UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId());

        UserDecorationInfoResult result = player.getModule(DecorationModule.class).getCurMaxDecorationInfo();
        int decorationClass = result.decorationClass;
        int decorationId = result.decorationId;
        long decorationOverTime = result.decorationOverTime;
        //称号加成
        int decorationAddition = ZsMgr.getDecorationAddition(decorationId);
        //直接上跨服
        ZhaoShangProto.CrossZsProjectSeatRobReqMsg.Builder crossReqMsg = ZhaoShangProto.CrossZsProjectSeatRobReqMsg.newBuilder();
        crossReqMsg.setProjectId(projectId);
        crossReqMsg.setSeatId(seatId);
        crossReqMsg.setSeatUserId(seatUserId);
        crossReqMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        crossReqMsg.setAddition(decorationAddition);
        crossReqMsg.setDecorationClass(decorationClass);
        crossReqMsg.setRobNumLimit(player.getModule(ZsModule.class).getZsRobNum());
        crossReqMsg.setTitleAddition(ZsMgr.getTitleAddition(player.getTitleId()));
        crossReqMsg.setCoolDownLimit(GameConfig.ZHAOSHANG_COOL_DOWN_LIMIT);
        crossReqMsg.setTemplateId(templateId);
        crossReqMsg.setGroupId(ZsMgr.getGroupId(GameServer.getInstance().getServerId()));  //分组id
        crossReqMsg.setProjectServerId(projectServerId);
        crossReqMsg.setDecorationOverTime(decorationOverTime);
        //抢占时间限制
        crossReqMsg.addTimeLimit(ConfigMgr.getZsProjectOpenTime().get(2));
        crossReqMsg.addTimeLimit(ConfigMgr.getZsProjectOpenTime().get(3));
        player.sendPacket(CrossProtocol.C2_ZHAO_SHANG_PROJECT_SEAT_ROB, crossReqMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            ZhaoShangProto.ZsProjectSeatRobRespMsg.Builder respMsg = ZhaoShangProto.ZsProjectSeatRobRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setProjectId(projectId);
            respMsg.setSeatId(seatId);
            player.sendPacket(Protocol.U_ZS_PROJECT_SEAT_ROB, respMsg);
        }
    }
}
